<template>
    <el-dialog v-model="dialogFormVisible" :title="title" @close="closeDialog">
        <el-form :model="addBaseArchievementScoreLineForm" ref="addBaseArchievementScoreLineRef" :rules="addBaseArchievementScoreLinerules">
                        <el-form-item label="文理科目" :label-width="formLabelWidth"
                                      prop="subjectType">
                            <el-input
                                    v-model="addBaseArchievementScoreLineForm.subjectType"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写文理科目"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="科目序号" :label-width="formLabelWidth"
                                      prop="subjectIndex" v-if="addBaseArchievementScoreLineForm.id">
                            <el-input
                                    v-model="addBaseArchievementScoreLineForm.subjectIndex"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写科目序号"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="科目名称" :label-width="formLabelWidth"
                                      prop="subjectName">
                            <el-input
                                    v-model="addBaseArchievementScoreLineForm.subjectName"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写科目名称"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="类别" :label-width="formLabelWidth"
                                      prop="category" v-if="addBaseArchievementScoreLineForm.id">
                            <el-input
                                    v-model="addBaseArchievementScoreLineForm.category"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写类别"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="类别2" :label-width="formLabelWidth"
                                      prop="subcategory" v-if="addBaseArchievementScoreLineForm.id">
                            <el-input
                                    v-model="addBaseArchievementScoreLineForm.subcategory"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写类别2"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="参考人数" :label-width="formLabelWidth"
                                      prop="referenceCount" v-if="addBaseArchievementScoreLineForm.id">
                            <el-input
                                    v-model="addBaseArchievementScoreLineForm.referenceCount"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写参考人数"
                            ></el-input>
                        </el-form-item>
                        <el-form-item label="分数线" :label-width="formLabelWidth"
                                      prop="scoreLine" v-if="addBaseArchievementScoreLineForm.id">
                            <el-input
                                    v-model="addBaseArchievementScoreLineForm.scoreLine"
                                    autocomplete="off"
                                    ref="name"
                                    placeholder="请填写分数线"
                            ></el-input>
                        </el-form-item>
        </el-form>
        <template #footer>
      <span class="dialog-footer">
        <el-button @click="cancel()">取消</el-button>
        <el-button type="primary" @click="save()">保存</el-button>
      </span>
        </template>
    </el-dialog>
</template>

<script>
    import {ref} from "vue";
    import {httpService} from "@/utils/httpService";
    import {ElMessage, ElForm} from "element-plus";
    import {vxRule} from "@/utils/validator";

    export default {
        name: "AddBaseArchievementScoreLine",
        components: {ElForm, ElMessage},
        data() {
            return {
                dialogFormVisible: ref(false),
                formLabelWidth: "120px",
                title: "",
                personnelType: "",
                addBaseArchievementScoreLineForm: {
                    subjectType: "",
                    subjectIndex: "",
                    subjectName: "",
                    category: "",
                    subcategory: "",
                    referenceCount: "",
                    scoreLine: "",
                },
                // 校验规则
                addBaseArchievementScoreLinerules: {
                    subjectType: vxRule(true, null, "blur"),
                    subjectName: vxRule(true, null, "blur"),
                },
            };
        },
        methods: {
            closeDialog() {
                this.clear();
            },
            cancel() {
                this.dialogFormVisible = false;
                this.clear();
            },
            getBaseArchievementScoreLineById(id) {
                httpService("/api/baseArchievementScoreLine/queryById", {id: id}, "get").then(
                    (data) => {
                        this.addBaseArchievementScoreLineForm = data;
                    }
                );
            },
            clear() {
                this.dialogFormVisible = false;
                //清空form
                    this.addBaseArchievementScoreLineForm.id = "";
                    this.addBaseArchievementScoreLineForm.subjectType = "";
                    this.addBaseArchievementScoreLineForm.subjectIndex = "";
                    this.addBaseArchievementScoreLineForm.subjectName = "";
                    this.addBaseArchievementScoreLineForm.category = "";
                    this.addBaseArchievementScoreLineForm.subcategory = "";
                    this.addBaseArchievementScoreLineForm.referenceCount = "";
                    this.addBaseArchievementScoreLineForm.scoreLine = "";
            },
            save() {
                this.$refs.addBaseArchievementScoreLineRef.validate((valid) => {
                    if (valid) {
                        let params = new FormData(); //创建一个form对象,必须是form对象否则后端接受不到数据
                        params.append("subjectType", this.addBaseArchievementScoreLineForm.subjectType);
                        params.append("subjectIndex", this.addBaseArchievementScoreLineForm.subjectIndex);
                        params.append("subjectName", this.addBaseArchievementScoreLineForm.subjectName);
                        params.append("category", this.addBaseArchievementScoreLineForm.category);
                        params.append("subcategory", this.addBaseArchievementScoreLineForm.subcategory);
                        params.append("referenceCount", this.addBaseArchievementScoreLineForm.referenceCount);
                        params.append("scoreLine", this.addBaseArchievementScoreLineForm.scoreLine);
                        if (this.addBaseArchievementScoreLineForm.id) {
                            params.append("id", this.addBaseArchievementScoreLineForm.id);
                            httpService("/api/baseArchievementScoreLine/edit", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("修改成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getBaseArchievementScoreLineList();
                                    } else {
                                        ElMessage.error("修改失败");
                                    }
                                }
                            );
                        } else {
                            httpService("/api/baseArchievementScoreLine/add", params, "post").then(
                                (data) => {
                                    if (data.code == 200) {
                                        ElMessage.success("添加成功");
                                        this.clear();
                                        //刷新父组件的数据
                                        this.$parent.getBaseArchievementScoreLineList();
                                    } else {
                                        ElMessage.error("添加失败");
                                    }
                                }
                            );
                        }
                    } else {
                        return false;
                    }
                });
            },
        },
    };
</script>
<style>
</style>